package com.ruoyi.system.service.impl;

import java.util.List;

import javax.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysLmUserMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysLmUser;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysLmUserService;

/**
 * 联盟成员信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-09
 */
@Service
public class SysLmUserServiceImpl implements ISysLmUserService 
{
	  private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
	   @Autowired
	    private ISysConfigService configService;
    @Autowired
    private SysLmUserMapper sysLmUserMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询联盟成员信息
     * 
     * @param uId 联盟成员信息主键
     * @return 联盟成员信息
     */
    @Override
    public SysLmUser selectSysLmUserByUId(Long uId)
    {
        return sysLmUserMapper.selectSysLmUserByUId(uId);
    }

    /**
     * 查询联盟成员信息列表
     * 
     * @param sysLmUser 联盟成员信息
     * @return 联盟成员信息
     */
    @Override
    public List<SysLmUser> selectSysLmUserList(SysLmUser sysLmUser)
    {
        return sysLmUserMapper.selectSysLmUserList(sysLmUser);
    }

    /**
     * 新增联盟成员信息
     * 
     * @param sysLmUser 联盟成员信息
     * @return 结果
     */
    @Override
    public int insertSysLmUser(SysLmUser sysLmUser)
    {
        return sysLmUserMapper.insertSysLmUser(sysLmUser);
    }

    /**
     * 修改联盟成员信息
     * 
     * @param sysLmUser 联盟成员信息
     * @return 结果
     */
    @Override
    public int updateSysLmUser(SysLmUser sysLmUser)
    {
        return sysLmUserMapper.updateSysLmUser(sysLmUser);
    }

    /**
     * 批量删除联盟成员信息
     * 
     * @param uIds 需要删除的联盟成员信息主键
     * @return 结果
     */
    @Override
    public int deleteSysLmUserByUIds(Long[] uIds)
    {
        return sysLmUserMapper.deleteSysLmUserByUIds(uIds);
    }

    /**
     * 删除联盟成员信息信息
     * 
     * @param uId 联盟成员信息主键
     * @return 结果
     */
    @Override
    public int deleteSysLmUserByUId(Long uId)
    {
        return sysLmUserMapper.deleteSysLmUserByUId(uId);
    }
    public void checkUserAllowed(SysUser user)
    {
        if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
        {
            throw new ServiceException("不允许操作超级管理员用户");
        }
    }
    
    /**
     * 导入用户数据
     * 
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    
    @Override
    public SysLmUser selectUserByUserName(String userName)
    {
        return sysLmUserMapper.selectUserByUserName(userName);
    }
    
    @Override
    public void checkUserAllowed(SysLmUser user)
    {
        if (StringUtils.isNotNull(user.getuId()))
        {
            throw new ServiceException("不允许操作超级管理员用户");
        }
    }
    @Override
    public String importUser(List<SysLmUser> userList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SysLmUser user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                SysLmUser u = sysLmUserMapper.selectUserByUserName(user.getName());
               
              
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setCreateBy(operName);
                    this.insertSysLmUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    checkUserAllowed(user);
                    checkUserDataScope(u.getuId());
                    user.setuId(u.getuId());
                    
                    System.err.println(user);
                    this.updateSysLmUser(user);
             
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 "+ " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 "+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

	private void checkUserDataScope(Long getuId) {
		// TODO Auto-generated method stub
		
	}
}
